import { asyncUniAPIWrap } from "@/utils/apiWrap";
import type { UniApiParam } from "@/typings/apiCallBack";
class Router {
    // 是否正在 导航
    _hasNavigate: boolean = false;

    // 导航
    _navigate(fn: string, d?: UniApiParam) {
        if (this._hasNavigate) {
            return;
        }
        let that = this;
        let { success, complete, fail, ...others } = d || {};
        asyncUniAPIWrap(fn, {
            ...others,
            success(res) {
                that._hasNavigate = false;
                success && success(res);
            },
            fail(err) {
                that._hasNavigate = false;
                fail && fail(err);
            },
            complete(res) {
                complete && complete(res);
            },
        });
    }

    navigateTo(d?: UniApiParam) {
        this._navigate("navigateTo", d);
    }

    // 返回
    navigateBack(d?: UniApiParam) {
        this._navigate("navigateBack", d);
    }
}

const router = new Router();

export { router };
